from random import randint
import os
import inspect

def __print_category(cat_name, sub_path='Testas-X', _type="GIFT"):

    if sub_path: cat_name = sub_path+'/'+cat_name
    
    if _type=="GIFT":
        print("\n$CATEGORY: $system$/%s\n" %  cat_name )
        
    if _type=="CLOZE":
        print("""
              <question type="category">
                <category> <text>$system$/%s</text> </category>
              </question>
              """     % ( cat_name)
        )

def __rand_questions(fun, args_intervals, how_many, sub_path='', _type="GIFT"):
    __print_category(fun.__name__+'_RANDOM', sub_path=sub_path, _type=_type)
    for i in range(how_many):
        args  = [randint(*interval) for interval in args_intervals]
        #~ fun( *args )
        __question(fun, args=args) 



###########  via decorators ############
def __parent_dir_2path():
    import os,sys,inspect
    currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    parentdir = os.path.dirname(currentdir)
    sys.path.insert(0,parentdir)  
__parent_dir_2path()
#~ generate_via_decorators = __import__("generate_via_decorators")

###################### NG (not finished) - MORE STRUCTURED question collecting approach ##########

def __category(cat_name, stack=None):
    if stack:
        stack.append( dict(cat_name=cat_name, questions=[] ) )
    else:
        __print_category(cat_name)   # legacy
    pass
    
def __question(fun, args, kwargs=None, stack=None):
    if kwargs is None: kwargs = {}
    
    if stack:
        if stack == []:
            __category('Unnamed', stack)
        #~ start_output_caching()
        #~ fun(  *args, **kwargs ) 
        #~ output = finish_output_caching()
        
        stack[-1]['questions'].append( dict(
           src =  inspect.getsource(fun),
           output = output ,
           #~ question =   ,
           #~ answer = 
        ))
    
    else:
        #~ print("***")
        #~ print(inspect.getsource(fun))
        fun(  *args, **kwargs )  # legacy
